<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                    <el-dropdown-item>删除子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot="scope">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!-- 1.1.父传子（isEdit判断编辑还是添加） -->
      <deptDialog
        v-if="showDialog"
        :pid="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @success="hsuccess"
        @close="hclose"
      /></el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { array2Tree } from '@/utils/index'
import deptDialog from './deptDialog.vue'
import { logger } from 'runjs/lib/common'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      // (2)写在数据中
      curId: '',
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      // 1.2.是编辑还是添加
      isEdit: false,
      // 用来传递给子组件做表单校验
      originList: []

    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async  loadDepartments() {
      // 这处有bug,解决方法是加上await ,出现原因是:要先请求数据,才可以找到depts
      const res = await getDepartments()
      res.data.depts.shift()
      this.list = array2Tree(res.data.depts, '')
      // ----------------------------------------------------------------------父组件准备列表数据,给子组件
      // 组装一份简单的列表数据,给子组件做表单校验
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          name: item.name,
          code: item.code
        }
      })
    },
    // -----------------------------------------------------------------编辑
    hEdit(id) {
      // 1.3.显示弹框
      this.showDialog = true
      // 1.4将当前编辑的部门id传给子组件
      this.curId = id
      // 1.5判断是编辑
      this.isEdit = true
      console.log(id)
    },
    hAdd(id) {
      // (1)获取当前被点击的部门的id
      this.curId = id
      // 点击添加部门,显示弹窗
      this.showDialog = true
      this.isEdit = false
    },
    hsuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // -------------------------------------------------------------------------------删除
    async  doDel(id) {
      await delDepartment(id)
      this.loadDepartments()
    },
    hDel(id) {
      // 弹层询问，是否退出
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {
        // 用户取消退出
      })
    },
    // ------------------------------------------------------父组件关闭弹层
    hclose() {
      this.showDialog = false
    }
  }
}
</script>
